package classes.graphicalEngine
{
	import flash.geom.Point;
	
	import mx.collections.*;
	import mx.core.*;
	import mx.events.MenuEvent;

	public class BackgroundLevelElement extends GameObject
	{
		static public var pool:ResourcePool = new ResourcePool(NewBackgroundLevelElement);

		// default map dimensions
		protected var defaultMapWidth:Number = GameObjectManager.defaultMapWidth;
		protected var defaultMapHeight:Number = GameObjectManager.defaultMapHeight;
		
		protected var positionGPS:Point = new Point(0,0);

		static public function NewBackgroundLevelElement():GameObject
		{
			return new 	BackgroundLevelElement();
		}
		
		public function BackgroundLevelElement()
		{
			
		}
		
		public function startupBackgroundLevelElement(bitmap:GraphicsResource, position:Point, z:int):void
		{
			startupGameObject(bitmap, position, z);
			updatePositionGPS();
			// this.scrollRate = scrollRate; 
		}
		
		public override function enterFrame(dt:Number):void
		{

			if (!GameCamera.position.equals(GameCamera.destination))
			{
				position.x -= GameCamera.xShifting;
				position.y -= GameCamera.yShifting;
			}
			/*
			if (position.x < 0 - defaultMapWidth / 4 ||
				position.x > 5 * defaultMapWidth / 4 ||
				position.y < 0 - defaultMapHeight / 4 ||
				position.y > 5 * defaultMapHeight / 4)
			{
				this.shutdown();
			}*/
		}

		// udate la position GPS par rapport à la position locale
		protected function updatePositionGPS():Point
		{
			positionGPS.x = int((GameCamera.position.x + position.x) / GameObjectManager.defaultGrapicsSizeMultiple);
			positionGPS.y = int((GameCamera.position.y + position.y) / GameObjectManager.defaultGrapicsSizeMultiple);
			return positionGPS;
		}

		protected function updateGPSMapTo(occupied:int):void
		{
			var newGPSAddress:Point = positionGPS;
				for (var j:int = 0; j < graphics.bitmap.height; j += GameObjectManager.defaultGrapicsSizeMultiple)
				{
					newGPSAddress.y = newGPSAddress.y + j;
					for (var k:int = 0; k < graphics.bitmap.width; k += GameObjectManager.defaultGrapicsSizeMultiple)
					{
						newGPSAddress.x = newGPSAddress.x + k;
						GameObjectManager.carteGPS[newGPSAddress.y][newGPSAddress.x] = occupied;
					}
				}
		}

	 // The event listener for the itemClick event.
        override protected function itemClickInfo(event:MenuEvent):void {
        	if (event.item.@label == "Cut")
        	{
        		//updateGPSMapTo(0);
        		//this.shutdown();
        		//GameObjectManager.needToBeRefreshed = true;
        	}
        	else
        	{
        		if (event.item.@label == "Collect")
        		{
        			if (objectName == "Cacao2") graphics = ResourceManager.Cacao1Graphics;
        			if (objectName == "Latex2") graphics = ResourceManager.Latex1Graphics;
        			if (objectName == "Soja") graphics = ResourceManager.ChampGraphics;
        			if (objectName == "Manioca") graphics = ResourceManager.ChampGraphics;
	        		AccordionBuild.addNewObject(objectName);
	        		GameObjectManager.needToBeRefreshed = true;
        		}
        	}
        	
            //Alert.show("\nItem label: " + event.item.@label);
        }

	}
}